package view;

import com.example.googlemarketdemo.R;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ProgressView extends LinearLayout {
	
	private ImageView ivIcon;
	private TextView tvNote;
	
	private long max = 100;
	private long progress ;
	
	private boolean isProgressEnable = true;
	
	/**
	 * 设置是否允许进度
	 * @param isProgressEnable
	 */
	public void setProgressEnable(boolean isProgressEnable) {
		this.isProgressEnable = isProgressEnable;
	}

	/**
	 * 设置最大值
	 * @param max
	 */
	public void setMax(long max) {
		this.max = max;
	}
	

	/**
	 * 设置当前进度
	 * @param progress
	 */
	public void setProgress(long progress) {
		this.progress = progress;
		//重绘操作
		invalidate();
	}

	/**
	 * 修改图标内容
	 * @param ivIcon
	 */
	public void setIcon(int resId) {
		ivIcon.setImageResource(resId);
	}

	/**
	 * 修改文字内容
	 * @param tvNote
	 */
	public void setNote(String note) {
		tvNote.setText(note);
	}

	

	public ProgressView(Context context) {
		this(context,null);
	}

	public ProgressView(Context context, AttributeSet attrs) {
		super(context, attrs);
		View view = View.inflate(context, R.layout.inflate_progressview, this);
		ivIcon = (ImageView) view.findViewById(R.id.ivicon);
		tvNote = (TextView) view.findViewById(R.id.tvNote);
	}

	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
	}
	
	@Override
	protected void dispatchDraw(Canvas canvas) {
		super.dispatchDraw(canvas);
		
		if (isProgressEnable) {
			Paint paint =new Paint();
			paint.setColor(getResources().getColor(R.color.progressView));
			paint.setAntiAlias(true);
			paint.setStrokeWidth(5);
			paint.setStyle(Style.STROKE);
			
			RectF oval = new RectF(ivIcon.getLeft(), ivIcon.getTop(), ivIcon.getRight(), ivIcon.getBottom());
			float startAngle = -90;
			float sweepAngle = progress*360.0f/max;
			boolean useCenter = false;
			canvas.drawArc(oval, startAngle, sweepAngle, useCenter, paint);
		}
		
	}

}
